package com.yhf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhf.bean.Admin;
import com.yhf.bean.Grade;
import com.yhf.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/grade")
public class GradeController {
    private Map<String, Object> result = new HashMap<>();

    @Autowired
    private GradeService gradeService;

    @GetMapping("/goGradeListView")
    public String goGradeListView() {
        return "grade/gradeList";
    }

    @PostMapping("/getGradeList")
    @ResponseBody
    public Map<String, Object> getGradeList(Integer page, Integer rows, String gradename) {

        Grade grade = new Grade();
        grade.setName(gradename);

        //设置每页记录数
        PageHelper.startPage(page, rows);


        //获取指定的和管理员或者是所有的管理员数据
        List<Grade> gradeList = gradeService.getGradeList(grade);

        PageInfo<Grade> gradePageInfo = new PageInfo<>(gradeList);
        //总页数

        long total = gradePageInfo.getTotal();
        result.put("total", total);
        System.out.println(gradeList);
        result.put("rows", gradeList);
        return result;

    }

    @PostMapping("/addGrade")
    @ResponseBody
    public Map<String, Object> addGrade(Grade grade) {
        Grade grade1 = gradeService.findByName(grade.getName());
        if (grade1 == null) {
            if (gradeService.addGrade(grade) > 0) {
                result.put("success", true);
            } else {
                result.put("success", false);
                result.put("msg", "服务器发生异常，添加失败！");
            }

        } else {
            result.put("success", false);
            result.put("msg", "用户名已存在");
        }
        return result;
    }

    @PostMapping("/editGrade")
    @ResponseBody
    public Map<String, Object> editGrade(Grade grade) {
        //需要排除用户名只修改账户名以外信息
        Grade grade1 = gradeService.findByName(grade.getName());
        if (grade1 != null) {
            if (!(grade.getId().equals(grade1.getId()))) {
                result.put("success", false);
                result.put("msg", "该用户名已存在，请更改重试！");
                return result;
            }
        }
//修改操作
        if (gradeService.updateGrade(grade) > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("msg", "服务器异常，修改失败！");
        }
        return result;

    }
    @PostMapping("/deleteGrade")
    @ResponseBody
    public Map<String, Object> deleteGrade(@RequestParam(value = "ids[]", required = true) Integer[] ids) {
        if (gradeService.deleteGrade(ids) > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("msg", "服务器异常，删除失败！");
        }
        return result;
    }

    }


